package com.visikard.business;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.visikard.application.Constant;
import com.visikard.net.VKregisterNetworkException;
import com.visikard.request.AccountRequest;
import com.visikard.response.BaseResponse;
import com.visikard.response.LogoutResponse;
import com.visikard.response.MerchantGetDashBoardInfoResponse;
import com.visikard.response.MerchantLoginResponse;
import com.visikard.response.SendEmailResponse;
import com.visikard.response.StaffLoginResponse;
import com.visikard.response.StaffRequestAccessResponse;
import com.visikard.utility.Utils;

public class AccountBUS {

	public MerchantLoginResponse merchantLoginApp(String username, String password) {

		MerchantLoginResponse result = new MerchantLoginResponse();

		AccountRequest ar = new AccountRequest();

		try {
			String response = ar.login(username, password, Constant.BOOLEAN_TRUE, "", "");
			result.extractDataFromServer(response);

		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "loginApp: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "loginApp: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}

		return result;
	}

	public StaffLoginResponse staffLoginApp(String username, String password, String merchantCode, String staffCode) {

		StaffLoginResponse result = new StaffLoginResponse();

		AccountRequest ar = new AccountRequest();

		try {
			String response = ar.login(username, password, Constant.BOOLEAN_FALSE, merchantCode, staffCode);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "loginApp: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "loginApp: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}

		return result;
	}

	public SendEmailResponse forgotPass(final String email) {
		AccountRequest ar = new AccountRequest();
		SendEmailResponse result = new SendEmailResponse();

		try {
			String response = ar.sendEmailVK(email);

			if (response != null && !response.isEmpty() && Utils.isValidJsonArray(response)) {
				JSONArray jarr = new JSONArray(response);
				JSONObject jobj = (JSONObject) jarr.get(0);
				String strResult = jobj.getString("messages");
				// response OK
				if (strResult.equals("EMPTY"))
					result.setErrorCode(SendEmailResponse.ERROR_EMAIL_NOT_EXISTS);
				else if (strResult.equals("INVALID SOCIAL NETWORK"))
					result.setErrorCode(SendEmailResponse.ERROR_INVALID_SOCIAL_NETWORK);
			}
			else {
				Log.e(Constant.LOG_TAG, "forgotPass: " + "Invalid response!!!");
				result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
				result.setErrorMsg("Invalid response!!!");
			}
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public LogoutResponse logout(long uid, String loginSession) {

		AccountRequest ar = new AccountRequest();
		LogoutResponse result = new LogoutResponse();

		try {
			ar.logout(uid, loginSession);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}

		return result;
	}

	public StaffRequestAccessResponse staffRequestAccess(String fname, String lname, String email, String merchantCode,
			String storeCode) {
		AccountRequest ar = new AccountRequest();
		StaffRequestAccessResponse result = new StaffRequestAccessResponse();

		try {
			String response = ar.staffRequestAccess(fname, lname, email, merchantCode, storeCode);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

	public MerchantGetDashBoardInfoResponse merchantGetDashBoardInfo(long merchantId, long idKards) {
		AccountRequest ar = new AccountRequest();
		MerchantGetDashBoardInfoResponse result = new MerchantGetDashBoardInfoResponse();

		try {
			String response = ar.merchantGetDashBoardInfo(merchantId, idKards);
			result.extractDataFromServer(response);
		}
		catch (VKregisterNetworkException e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.NETWORK_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "forgotPass: " + e.getMessage());
			result.setErrorCode(BaseResponse.UNKNOWN_ERROR);
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}

}
